#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>

struct BTNode
{
	int val;
	struct BTNode* left;
	struct BTNode* right;
};

struct BTNode* BTPreOrderCreat(char* str, int* pi)
{
	if (str[*pi] == '#')
	{
		(*pi)++;
		return NULL;
	}
	struct BTNode* root = (struct BTNode*)malloc(sizeof(struct BTNode));
	root->val = str[(*pi)++];
	root->left = BTPreOrderCreat(str, pi);
	root->right = BTPreOrderCreat(str, pi);
	return root;
}

void BTPreOrder(struct BTNode* root)
{
	if (root == NULL)
	{
		printf("#");
		return;
	}
	printf("%c", root->val);
	BTPreOrder(root->left);
	BTPreOrder(root->right);
}
struct BTNode* BTDestory(struct BTNode* root)
{
	if (root == NULL)
		return NULL;
	root->left = BTDestory(root->left);
	root->right = BTDestory(root->right);
	free(root);
	return NULL;
}
int main()
{
	char str[] = "ABD##E#H##CF##G##";
	int i = 0;
	//char str[101] = { 0 };
	//int i = 0;
	//while (scanf("%c", &str[i++]) != EOF);
	//i = 0;
	struct BTNode* root = BTPreOrderCreat(str, &i);
	BTPreOrder(root);
	BTDestory(root);
	return 0;
}